﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Data;
using System.ComponentModel;
using Microsoft.Phone.Controls.Maps;

namespace Strava1
{
    public partial class RideDetails : PhoneApplicationPage
    {
        public int RideId;

        public RideDetails()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(RideDetails_Loaded);
            /*this.slider.ManipulationStarted += new EventHandler<ManipulationStartedEventArgs>(slider_ManipulationStarted);
            Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);*/
        }

        void RideDetails_Loaded(object sender, RoutedEventArgs e)
        {
            effortsLB.SelectionChanged += new SelectionChangedEventHandler(Segments_SelectionChanged);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            RideId = int.Parse(NavigationContext.QueryString["id"]);
            Ride ride = App.ViewModel.Rides.First(r => r.Id == RideId);

            if (ride.Efforts == null)
            {
                ride.PopulateEfforts();
            }

            foreach (Effort effort in ride.Efforts)
            {
                effort.Segment.PopulateMyEfforts();
            }

            if (ride.EffortsCVS == null)
            {
                ride.EffortsCVS = new CollectionViewSource();
                ride.EffortsCVS.Source = ride.Efforts;
                ride.EffortsCVS.SortDescriptions.Add(new SortDescription("MyPlacing", ListSortDirection.Ascending));
                ride.EffortsCVS.SortDescriptions.Add(new SortDescription("Segment.PlacingCount", ListSortDirection.Descending));
                ride.EffortsCVS.SortDescriptions.Add(new SortDescription("Segment.ElevationDifference", ListSortDirection.Descending));
                ride.EffortsCVS.View.MoveCurrentToPosition(-1);
            }
            ride.EffortsCVS.View.Refresh();

            if (ride.MapLocations == null)
            {
                ride.PopulateMap();
            }

            ride.PopulateElevation();
            ride.CalculatePositions("Overall");

            DataContext = App.ViewModel;
            LayoutRoot.DataContext = ride;
            
            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
            {
                
            }
        }

        /*void Touch_FrameReported(object sender, TouchFrameEventArgs e)
        {
            if (e.GetPrimaryTouchPoint(this.slider).Action == TouchAction.Up)
            {
                myPivot.IsHitTestVisible = true;
            }
        }

        void slider_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            myPivot.IsHitTestVisible = false;
        }*/
        private void SetSegmentOrder_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Segments_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                NavigationService.Navigate(new Uri("/SegmentDetails.xaml?id=" + ((Effort)e.AddedItems[0]).SegmentId, UriKind.Relative));
            }
            ((ListBox)sender).SelectedIndex = -1;
        }

        private void MapPolyline_Loaded(object sender, RoutedEventArgs e)
        {
            MapPolyline mpl = (MapPolyline)sender;
            mpl.Stroke = ((MapPolylineObj)mpl.DataContext).Stroke;
            mpl.StrokeThickness = ((MapPolylineObj)mpl.DataContext).StrokeThickness;
        }
    }
}